﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace NimbleTextKeygen
{
    public class Log
    {
        public static string DefaultLogFilename;

        public static string LogFilename;

        private static bool s_writeToLog;

        static Log()
        {
            Log.DefaultLogFilename = Path.Combine(Resources.GetSettingsPath(), string.Concat(AssemblyAttributes.ProductName, ".log"));//AssemblyAttributes.ProductName 原代码是 Application.ProductName，此处替换后不影响
            Log.LogFilename = null;
            Log.s_writeToLog = true;
        }

        public static void ShowLog()
        {
            Log.ShowLog(Log.LogFilename);
        }

        public static void ShowLog(string fileName)
        {
            if (!File.Exists(fileName))
            {
                //MessageBox.Show("No logfile generated.", string.Concat(MediaTypeNames.Application.ProductName, " - Logfile not found"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo()
                {
                    UseShellExecute = true,
                    FileName = fileName
                };
                try
                {
                    processStartInfo.Verb = "Open";
                    Process.Start(processStartInfo);
                }
                catch (Win32Exception win32Exception1)
                {
                    Win32Exception win32Exception = win32Exception1;
                    try
                    {
                        Process.Start("notepad", fileName);
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        //MessageBox.Show(string.Concat("There was a problem while trying to view the log file using your default log file editor.\r\nWhen that happened we decided to try using 'NotePad' to open the file.\r\n\r\nThat failed too.\r\nHere's the two errors:\r\n", win32Exception.Message, "And Secondly:\r\n", exception.Message), string.Concat(MediaTypeNames.Application.ProductName, " - Error showing log"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception exception3)
                {
                    Exception exception2 = exception3;
                    //MessageBox.Show(string.Concat("There was a problem while loading your default log file editor.\r\n", exception2.Message), string.Concat(MediaTypeNames.Application.ProductName, " - Error showing log"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        public static void Write(string text)
        {
            StreamWriter streamWriter = null;
            if (!Log.s_writeToLog)
            {
                return;
            }
            try
            {
                try
                {
                    if (Log.LogFilename == null)
                    {
                        Log.LogFilename = Log.DefaultLogFilename;
                    }
                    IOUtils.EnsurePathExists(Path.GetDirectoryName(Log.LogFilename));
                    streamWriter = File.AppendText(Log.LogFilename);
                    streamWriter.Write(string.Format("[{0:yyyy-MM-dd HH:mm:ss}] {1}", DateTime.Now, text));
                }
                catch (Exception exception)
                {
                    Log.s_writeToLog = false;
                }
            }
            finally
            {
                try
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }
                }
                catch (IOException oException)
                {
                    Log.s_writeToLog = false;
                }
            }
        }

        public static void WriteLine(Exception ex, string message)
        {
            Log.Write(string.Concat(message, ":..."));
            Log.WriteLine(ex);
        }

        public static void WriteLine(Exception ex)
        {
            Log.WriteLine(ex.ToString());
            if (ex.InnerException != null)
            {
                Log.Write("Inner exception:...");
                Log.WriteLine(ex.InnerException);
            }
        }

        public static void WriteLine(string text)
        {
            Log.Write(string.Concat(text, Environment.NewLine));
        }

        public static void WriteLine(string format, params object[] args)
        {
            Log.WriteLine(string.Format(format, args));
        }
    }
}
